package com.lineage.data.item_etcitem.add;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.ItemVIPTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1ItemVIP;
import java.util.Iterator;
import java.util.List;

public class VIP extends ItemExecutor {
  public static ItemExecutor get() {
    return new VIP();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int item_id = item.getItemId();
    L1ItemVIP vip = ItemVIPTable.get().getVIP(item_id);
    if (vip == null)
      return; 
    if (item.isEquipped()) {
      item.setEquipped(false);
      ItemVIPTable.get().deleItemVIP(pc, item_id);
      pc.getInventory().updateItem(item, 8);
      pc.getInventory().saveItem(item, 8);
    } else {
      boolean isvip = false;
      List<L1ItemInstance> items = pc.getInventory().getItems();
      Iterator<L1ItemInstance> iterator = items.iterator();
      while (iterator.hasNext()) {
        L1ItemInstance tgitem = iterator.next();
        if (!tgitem.isEquipped())
          continue; 
        int tgitem_id = tgitem.getItemId();
        L1ItemVIP tgvip = ItemVIPTable.get().getVIP(tgitem_id);
        if (tgvip == null)
          continue; 
        if (tgvip.get_type() == vip.get_type()) {
          isvip = true;
          break;
        } 
      } 
      if (isvip) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(66));
      } else {
        item.setEquipped(true);
        ItemVIPTable.get().addItemVIP(pc, item_id);
        pc.getInventory().updateItem(item, 8);
        pc.getInventory().saveItem(item, 8);
      } 
    } 
  }
}
